package com.ruoyi.web.controller.workJob;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.worker.domain.*;
import com.ruoyi.worker.service.OperationNeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName: ruoyi
 * @Package: com.ruoyi.web.controller.operation
 * @ClassName: OperationNeedController
 * @Author: 胡志明
 * @Description:
 * @Date: 2021/9/9 20:28
 */
@RestController
@RequestMapping("/operation/need")
public class OperationNeedController extends BaseController {

    @Autowired
    private OperationNeedService operationNeedService;

    /**
     * 获取用工需求列表
     * @param qualityPredictPojo
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:qualityPredict:list')")
    @GetMapping("/getQualityPredictList")
    public TableDataInfo getQualityPredictList(QualityPredictPojo qualityPredictPojo)
    {
        startPage();
        List<QualityPredictPojo> qualityPredictPojoList = operationNeedService.getQualityPredictList(qualityPredictPojo);
        return getDataTable(qualityPredictPojoList);
    }

    /**
     * 获取用户信息
     * @param
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:qualityPredict:list')")
    @GetMapping("/viewUserData")
    public AjaxResult viewUserData()
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        QualityPredictPojo qualityPredictPojo = operationNeedService.viewUserData(userId.intValue());
        return AjaxResult.success(qualityPredictPojo);
    }

    /**
     * 获取用户信息
     * @param
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:qualityPredict:list')")
    @GetMapping("/viewTimeShift")
    public AjaxResult viewTimeShift()
    {
        List<Map<String,Object>> list = operationNeedService.viewTimeShift();
        return AjaxResult.success(list);
    }

    /**
     * 审核
     * @param qualityPredictPojo
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:operationNeed:audit')")
    @PostMapping("/auditQualityPredict")
    public AjaxResult auditQualityPredict(@RequestBody QualityPredictPojo qualityPredictPojo){
        Integer taskNode = qualityPredictPojo.getTaskNode();
        operationNeedService.auditQualityPredict(qualityPredictPojo);
        if (taskNode == 1){
            return AjaxResult.success("审核通过");
        }else if (taskNode == 2){
            return AjaxResult.success("审核驳回");
        }
        return AjaxResult.success();
    }

    @PostMapping("/addQualityPredict")
    public AjaxResult addQualityPredict(@RequestBody JSONObject jsonObject){
        int checkTaskNumber = operationNeedService.addQualityPredict(jsonObject);
        return AjaxResult.success("新增成功");
    }

    /**
     * 质量预测详情展示
     * @param qualityPredictId
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:operationNeed:view')")
    @GetMapping(value = { "/viewQualityPredict/{qualityPredictId}" })
    public AjaxResult viewQualityPredict(@PathVariable(value = "qualityPredictId", required = false) Long qualityPredictId){
        QualityPredictPojo qualityPredictPojo = new QualityPredictPojo();
        qualityPredictPojo.setQualityPredictId(Integer.parseInt(qualityPredictId+""));
        List<QualityPredictPojo> qualityPredictPojoList = operationNeedService.getQualityPredictList(qualityPredictPojo);
        return AjaxResult.success(qualityPredictPojoList.get(0));
    }

    /**
     * 删除数据
     * @param qualityPredictId
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:operationNeed:delete')")
    @DeleteMapping("/deleteQualityPredict/{qualityPredictId}")
    public AjaxResult deleteQualityPredict(@PathVariable Long qualityPredictId){
        operationNeedService.deleteOperationNeed(Integer.parseInt(qualityPredictId+""));
        operationNeedService.deleteQualityPredict(Integer.parseInt(qualityPredictId+""));
        return AjaxResult.success();
    }

    /**
     * 修改用工需求数据
     * @param jsonObject
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:operationNeed:update')")
    @PostMapping("/updateQualityPredict")
    public AjaxResult updateQualityPredict(@RequestBody JSONObject jsonObject){
        operationNeedService.updateQualityPredict(jsonObject);
        return AjaxResult.success();
    }

    /**
     * 获取用工需求列表
     * @param operationNeedPojo
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:operationNeed:list')")
    @GetMapping("/getOperationNeedList")
    public TableDataInfo getOperationNeedList(OperationNeedPojo operationNeedPojo)
    {
        startPage();
        List<OperationNeedPojo> operationNeedList = operationNeedService.getOperationNeedList(operationNeedPojo);
        return getDataTable(operationNeedList);
    }

    /**
     * 获取班次、岗位数据
     * @return
     */
    @GetMapping("/addShowOperationNeed")
    public AjaxResult addShowOperationNeed(){
        JSONArray JsonArray = operationNeedService.addShowOperationNeed();
        return AjaxResult.success(JsonArray);
    }

    /**
     * 新增用工需求数据
     * @param jsonObject
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:operationNeed:add')")
    @PostMapping("/addOperationNeed")
    public AjaxResult addOperationNeed(@RequestBody JSONObject jsonObject){
        int checkTaskNumber = operationNeedService.addOperationNeed(jsonObject);
//        if (checkTaskNumber == 1){
//            return AjaxResult.success("用工单号已存在");
//        }
//        if (checkTaskNumber == 2){
//            return AjaxResult.success("用工时间已存在");
//        }
        return AjaxResult.success("新增成功");
    }

    /**
     * 删除用工需求数据
     * @param operationNeedId
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:operationNeed:delete')")
    @DeleteMapping("/deleteOperationNeed/{operationNeedId}")
    public AjaxResult deleteOperationNeed(@PathVariable Long operationNeedId){
        operationNeedService.deleteOperationNeed(Integer.parseInt(operationNeedId+""));
        return AjaxResult.success();
    }

    /**
     * 用工需求详情展示
     * @param operationNeedId
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:operationNeed:view')")
    @GetMapping(value = { "/viewOperationNeed/{operationNeedId}" })
    public AjaxResult viewOperationNeed(@PathVariable(value = "operationNeedId", required = false) Long operationNeedId){
        JSONObject jsonObject = operationNeedService.viewOperationNeed(Integer.parseInt(operationNeedId+""));
        return AjaxResult.success(jsonObject);
    }

    /**
     * 审核用工需求
     * @param operateNeedVaryPojo
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:operationNeed:audit')")
    @PostMapping("/auditOperationNeed")
    public AjaxResult auditOperationNeed(@RequestBody OperateNeedVaryPojo operateNeedVaryPojo){
        Integer taskNode = operateNeedVaryPojo.getTaskNode();
        operationNeedService.auditOperationNeed(operateNeedVaryPojo);
        if (taskNode == 1){
            return AjaxResult.success("审核通过");
        }else if (taskNode == 2){
            return AjaxResult.success("审核驳回");
        }
        return AjaxResult.success();
    }

    /**
     * 修改用工需求数据
     * @param jsonObject
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:operationNeed:update')")
    @PostMapping("/updateOperationNeed")
    public AjaxResult updateOperationNeed(@RequestBody JSONObject jsonObject){
        operationNeedService.updateOperationNeed(jsonObject);
        return AjaxResult.success();
    }

    /**
     * 获取渠道人数配置数据
     * @param needTime
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:staffAssignments:list')")
    @GetMapping("/getStaffAssignmentsList")
    public TableDataInfo getStaffAssignmentsList(String needTime) {
        startPage();
        List<StaffAssignmentsPojo> staffAssignmentsList = operationNeedService.getStaffAssignmentsList(needTime);
        return getDataTable(staffAssignmentsList);
    }

    /**
     *  分配渠道人数审核
     * @param staffAssignmentsPojo
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:staffAssignments:audit')")
    @PostMapping("/auditStaffAssignments")
    public AjaxResult auditStaffAssignments(@RequestBody StaffAssignmentsPojo staffAssignmentsPojo) throws Exception {
        Integer taskNode = staffAssignmentsPojo.getTaskNode();
        operationNeedService.auditStaffAssignments(staffAssignmentsPojo);
        if (taskNode == 1){
            return AjaxResult.success("审核通过");
        }else if (taskNode == 2){
            return AjaxResult.success("审核驳回");
        }
        return AjaxResult.success();
    }

    @PostMapping("/testSaveRedis")
    public AjaxResult setRedisCache(@RequestBody StaffAssignmentsPojo staffAssignmentsPojo) throws Exception {
        operationNeedService.setRedisCache(staffAssignmentsPojo);
        return AjaxResult.success();
    }

    /**
     * 删除用工需求数据
     * @param staffAssignmentsId
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:staffAssignments:delete')")
    @DeleteMapping("/deleteStaffAssignments/{staffAssignmentsId}")
    public AjaxResult deleteStaffAssignments(@PathVariable Long staffAssignmentsId){
        operationNeedService.deleteStaffAssignments(Integer.parseInt(staffAssignmentsId+""));
        return AjaxResult.success();
    }

    /**
     * 渠道人数配置详情
     * @param staffAssignmentsId
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:staffAssignments:view')")
    @GetMapping(value = { "/viewStaffAssignments/{staffAssignmentsId}" })
    public AjaxResult viewStaffAssignments(@PathVariable(value = "staffAssignmentsId", required = false) Long staffAssignmentsId){
        JSONObject jsonObject = operationNeedService.viewStaffAssignments(Integer.parseInt(staffAssignmentsId+""));
        return AjaxResult.success(jsonObject);
    }

    /**
     * 修改渠道人数配置
     * @param jsonObject
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:staffAssignments:update')")
    @PostMapping("/updateStaffAssignments")
    public AjaxResult updateStaffAssignments(@RequestBody JSONObject jsonObject){
        operationNeedService.updateStaffAssignments(jsonObject);
        return AjaxResult.success();
    }

    /**
     * 获取用工需求列表
     * @param workerNeedPojo
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:workerNeed:list')")
    @GetMapping("/getWorkerNeedList")
    public TableDataInfo getWorkerNeedList(WorkerNeedPojo workerNeedPojo)
    {
        startPage();
        List<WorkerNeedPojo> workerNeedList = operationNeedService.getWorkerNeedList(workerNeedPojo);
        return getDataTable(workerNeedList);
    }

    /**
     * 作废用工需求数据
     * @param workerNeedId
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:workerNeed:delete')")
    @DeleteMapping("/deleteWorkerNeed/{workerNeedId}")
    public AjaxResult deleteWorkerNeed(@PathVariable Long workerNeedId){
        operationNeedService.deleteWorkerNeed(Integer.parseInt(workerNeedId+""));
        return AjaxResult.success();
    }

    /**
     * 审核用工需求
     * @param workerNeedPojo
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:workerNeed:audit')")
    @PostMapping("/auditWorkerNeed")
    public AjaxResult auditWorkerNeed(@RequestBody WorkerNeedPojo workerNeedPojo){
        Integer documentStatus = workerNeedPojo.getDocumentStatus();
        operationNeedService.auditWorkerNeed(workerNeedPojo);
        if (documentStatus == 2){
            return AjaxResult.success("审核通过");
        }else if (documentStatus == 0){
            return AjaxResult.success("审核驳回");
        }
        return AjaxResult.success();
    }

    /**
     * 用工需求列表详情
     * @param workerNeedId
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('job:workerNeed:view')")
    @GetMapping(value = { "/viewWorkerNeed/{workerNeedId}" })
    public AjaxResult viewWorkerNeed(@PathVariable(value = "workerNeedId", required = false) Long workerNeedId){
        JSONObject jsonObject = operationNeedService.viewWorkerNeed(Integer.parseInt(workerNeedId+""));
        return AjaxResult.success(jsonObject);
    }

    /**
     * 用工需求测算
     * @param jsonObject
     * @return
     */
    @PostMapping("/updateWorkerNeed")
    public AjaxResult updateWorkerNeed(@RequestBody JSONObject jsonObject){
        operationNeedService.updateWorkerNeed(jsonObject);
        return AjaxResult.success();
    }
}
